//
//  HeaderCollectionViewCell.m
//  miaopai
//
//  Created by Crow Zhang on 15/11/18.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "HeaderCollectionViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>

@interface HeaderCollectionViewCell()

@property(nonatomic, retain)UIImageView *pic;

@property(nonatomic, retain)UILabel *headLabel;

@property(nonatomic, retain)UIView *backView;

@property(nonatomic, retain)UILabel *imagineLabel;

@property(nonatomic, retain)UIImageView *videoPic;

@property(nonatomic, retain)UILabel *videoLabel;

@end

@implementation HeaderCollectionViewCell

- (void)dealloc
{
    [_pic release];
    [_headLabel release];
    [_backView release];
    [_imagineLabel release];
    [_videoPic release];
    [_videoLabel release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}

- (void)creatView
{
    self.pic = [[UIImageView alloc] init];
    [self.contentView addSubview:self.pic];
    [_pic release];
    
    self.headLabel = [[UILabel alloc] init];
    self.headLabel.textColor = [UIColor whiteColor];
    self.headLabel.textAlignment = NSTextAlignmentCenter;
    self.headLabel.font = [UIFont systemFontOfSize:13];
    [self.pic addSubview:self.headLabel];
    [_headLabel release];
    
    self.backView = [[UIView alloc] init];
    self.backView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.backView];
    [_backView release];
    
    self.imagineLabel = [[UILabel alloc] init];
    self.imagineLabel.text = @"推荐视频";
    self.imagineLabel.layer.masksToBounds = YES;
    self.imagineLabel.layer.cornerRadius = 15;
    self.imagineLabel.layer.borderWidth = 1;
    self.imagineLabel.layer.borderColor = [[UIColor grayColor] CGColor];
    self.imagineLabel.backgroundColor = [UIColor whiteColor];
    self.imagineLabel.textAlignment = NSTextAlignmentCenter;
    [self.backView addSubview:self.imagineLabel];
    [_imagineLabel release];
    
    self.videoPic = [[UIImageView alloc] init];
    [self.backView addSubview:self.videoPic];
    [_videoPic release];
    
    self.videoLabel = [[UILabel alloc] init];
    self.videoLabel.textAlignment = NSTextAlignmentCenter;
    self.videoLabel.font = [UIFont systemFontOfSize:14];
    self.videoLabel.numberOfLines = 0;
    self.videoLabel.textColor = [UIColor grayColor];
    [self.backView addSubview:self.videoLabel];
    [_videoLabel release];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.pic.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 1.5 * 0.25);
    self.headLabel.frame = CGRectMake(0, self.pic.frame.size.height - 30, self.pic.frame.size.width, 30);
    self.backView.frame = CGRectMake( 10, self.frame.size.height / 1.5 * 0.25 + 30, self.frame.size.width - 20, self.frame.size.height - (self.frame.size.height / 1.5 * 0.25 + 30) - 10);
    self.imagineLabel.frame = CGRectMake(self.backView.frame.size.width / 2 - 50, -20, 100, 40);
    self.videoPic.frame = CGRectMake(10, 30, self.backView.frame.size.width - 20, self.backView.frame.size.width - 20);
    self.videoLabel.frame = CGRectMake(10, self.backView.frame.size.width + 10, self.backView.frame.size.width - 20, self.backView.frame.size.height - self.backView.frame.size.width - 10);
}

- (void)setTopicDetail:(TopicDetailModel *)topicDetail
{
    if (_topicDetail != topicDetail) {
        [_topicDetail release];
        _topicDetail = [topicDetail retain];
    }
    [self.pic sd_setImageWithURL:[NSURL URLWithString:topicDetail.pic] placeholderImage:[UIImage imageNamed:@"topic_bg"]];
    NSLog(@"%@", topicDetail.pic);
    NSString *channelCnt = nil;
    NSString *likeCnt = nil;
    NSString *commentCnt = nil;
    if ([topicDetail.channelCnt integerValue] > 10000) {
        channelCnt = [NSString stringWithFormat:@"%.1lf万", [topicDetail.channelCnt integerValue] / 10000.0];
    }else
    {
        channelCnt = [NSString stringWithFormat:@"%@", topicDetail.channelCnt];
    }
    if ([topicDetail.likeCnt integerValue] > 10000) {
        likeCnt = [NSString stringWithFormat:@"%.1lf万", [topicDetail.likeCnt integerValue] / 10000.0];
    }else
    {
        likeCnt = [NSString stringWithFormat:@"%@", topicDetail.likeCnt];
    }
    if ([topicDetail.commentCnt integerValue] > 10000) {
        commentCnt = [NSString stringWithFormat:@"%.1lf万", [topicDetail.commentCnt integerValue] / 10000.0];
    }else
    {
        commentCnt = [NSString stringWithFormat:@"%@", topicDetail.commentCnt];
    }
    self.headLabel.text = [NSString stringWithFormat:@"喜欢 %@  评论 %@  视频 %@", likeCnt, commentCnt, channelCnt];
    [self.videoPic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", topicDetail.channel.pic.base, topicDetail.channel.pic.m]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    
    self.videoLabel.text = topicDetail.channel.ext.t;
}

@end
